package com.py.credit.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.IndexCatModel;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.common.OptionalColumn;
import com.py.credit.entity.enums.ResourceModelFieldsEnum;
import com.py.credit.service.IndexService;
import com.py.credit.service.ModelBaseService;
import com.py.credit.service.XypjRemoteBaseService;
import com.py.credit.utils.ExcelUtil;
import com.py.credit.utils.MapCache;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.HttpServletHelper;

@RestController
@RequestMapping(value = "/modelBase")
public class ModelBaseController {

	private static final int ZERO = 0;

	private static final String ZERO_STR = "0";

	private final static String FRONT_TABLE_ID = "modelBase";

	@Autowired
	private ModelBaseService modelBaseService;

	@Autowired
	private XypjRemoteBaseService xypjRemoteBaseService;
	@Autowired
	private IndexService indexService;

	/**
	 * 获取初始化列
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getOptionalColums.json")
	public RestServiceResult<List<OptionalColumn>> getOptionalColums() {
		RestServiceResult<List<OptionalColumn>> r = new RestServiceResult<List<OptionalColumn>>();
		try {
			r = new RestServiceResult<List<OptionalColumn>>(ResourceModelFieldsEnum.getOptionalColumns());
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常");
		}
		return r;
	}

	/**
	 * @api {get} /shareLog/getTableList.json 获取表规则列表
	 * @apiName getTableList
	 * @apiVersion 0.0.1
	 * @apiGroup 表规则管理
	 * 
	 * @apiParam {String} condition 查询条件(可选)
	 * @apiParam {int} limit 每页展示条数 (必需)
	 * @apiParam {int} page 当前页数 (必需)
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":2,"data":[{"rule_package":"1222","rule_desc":"122222","rule_name":"1","rule_method":"123","id":22,"time":"2018-04-23","managerName":"超级管理员"},{"rule_package":"555","rule_desc":"5555","rule_name":"555","rule_method":"555","id":35,"time":"2018-04-26","managerName":"超级管理员"}]}
	 * 
	 */
	@RequestMapping(value = "/getTableList.json")
	public RestServiceResult<List<Map<String, Object>>> getTableList(
			@RequestParam(value = "condition", defaultValue = "") String condition, String kId, String areaId,
			String industryId, Page page, HttpServletRequest request) {

		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		String[] additionalColumns = { "id,mainType,countXypj,countOrgModel,step,scoreType,rate" };
		String[] configColumns = (String[]) MapCache.get(userInfo.getId() + "_" + FRONT_TABLE_ID);
		List<String> showColumns = (configColumns == null || configColumns.length == 0)
				? ResourceModelFieldsEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);
		// condition="[{columnEN:\"batch_id\",value:\"EX-test-1-20180326-1174\",type:\"varchar\",fuzzy:fasle}]";
		RestServiceResult<List<Map<String, Object>>> r = new RestServiceResult<List<Map<String, Object>>>();
		try {
			List<Map<String, Object>> tableList = modelBaseService.getByConditionForPage(showColumns, additionalColumns,
					condition, page, kId, areaId, industryId);
			r = new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

	@RequestMapping(value = "/export")
	public RestServiceResult<List<Map<String, Object>>> exportData(
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
			String kId, String areaId, String industryId, HttpServletResponse response, HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		page.setLimit(1000);
		String[] configColumns = (String[]) MapCache.get(1000 + "_" + FRONT_TABLE_ID);
		List<String> showColumns = (configColumns == null || configColumns.length == 0)
				? ResourceModelFieldsEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);
		List<Map<String, Object>> tableList = modelBaseService.getByConditionForPage(showColumns, null, condition, page,
				kId, areaId, industryId);
		try {
			ExcelUtil.downloadExcel(response, tableList, "模型管理", "模型管理", showColumns,
					ResourceModelFieldsEnum.getOptionalColumns());
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("模型导出异常，请稍后再试！");
			ExceptionLogger.error("模型导出异常！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/getAdvancedConditionColumns.json")
	public RestServiceResult<List<OptionalColumn>> getAdvancedConditionColumns() {
		RestServiceResult<List<OptionalColumn>> r = new RestServiceResult<List<OptionalColumn>>();
		try {
			r = new RestServiceResult<List<OptionalColumn>>(ResourceModelFieldsEnum.getAdvancedConditionColumns());
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常");
		}
		return r;

	}

	/**
	 * 手动选择展示列
	 * 
	 * @param columns
	 * @return
	 */
	@RequestMapping(value = "/addShowColunms.json")
	public RestServiceResult<String> addShowColunms(@RequestParam(value = "columns", required = true) String[] columns,
			HttpServletRequest request) {
		RestServiceResult<String> result = new RestServiceResult<String>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("AchievementController#addShowColunms", "未获取到当前用户信息！");
				return result;
			}
			String column = StringUtils.join(columns, ",");
			MapCache.set(userInfo.getId() + "_" + FRONT_TABLE_ID, StringUtils.split(column, ","));
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		return result;
	}

	/**
	 * @api {get} /shareLog/add.json 新增规则
	 * @apiName add
	 * @apiVersion 0.0.1
	 * @apiGroup 共享日志新增
	 * 
	 * @apiParam {String} sortType 分类方式 1、一站式 2、信用核查 3、外部接口
	 * @apiParam {String} searchCatalog 查询目录
	 * @apiParam {String} ip 访问ip
	 * @apiParam {String} usernme 访问用户名
	 * @apiParam {String} realname 真实姓名
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":0,"data":[{}]}
	 * 
	 */
	@ResponseBody
	@RequestMapping(value = "/saveOrUpdate.json")
	public RestServiceResult<String> add(String id, String bm, String name, String des, String scoreType, Double rate,
			String zone, String industry, String reportType, String mainType, String relatedModel,
			HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<String> result = new RestServiceResult<String>();
		try {

			// 获取当前用户信息
			RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
			SysUser userInfo = null;
			if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
				userInfo = user.getData();
				if (null == userInfo || userInfo.getId() <= 0) {
					result.setCode(RestServiceResult.CODE_UNKNOW);
					result.setMsg("系统异常，请稍后再试！");
					ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
					return result;
				}
			} else {
				result.setCode(user.getCode());
				result.setMsg(user.getMsg());
				return result;
			}
			id = modelBaseService.saveOrUpdate(id, bm, name, des, scoreType, rate, zone, industry, reportType, mainType,
					relatedModel, "2");
			result.setData(id);
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("添加或修改模型异常");
			ExceptionLogger.error("添加或修改模型 异常！", e.getMessage(), e);
		}
		return result;
	}

	/**
	 * @api {get} /shareLog/add.json 新增规则
	 * @apiName add
	 * @apiVersion 0.0.1
	 * @apiGroup 共享日志新增
	 * 
	 * @apiParam {String} sortType 分类方式 1、一站式 2、信用核查 3、外部接口
	 * @apiParam {String} searchCatalog 查询目录
	 * @apiParam {String} ip 访问ip
	 * @apiParam {String} usernme 访问用户名
	 * @apiParam {String} realname 真实姓名
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":0,"data":[{}]}
	 * 
	 */
	@ResponseBody
	@RequestMapping(value = "/updateProcess.do")
	public RestServiceResult<String> add(String id, String templateType, String checkType, String checkProcess,
			HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<String> result = new RestServiceResult<String>();
		try {

			// 获取当前用户信息
			RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
			SysUser userInfo = null;
			if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
				userInfo = user.getData();
				if (null == userInfo || userInfo.getId() <= 0) {
					result.setCode(RestServiceResult.CODE_UNKNOW);
					result.setMsg("系统异常，请稍后再试！");
					ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
					return result;
				}
			} else {
				result.setCode(user.getCode());
				result.setMsg(user.getMsg());
				return result;
			}
			modelBaseService.updateProcess(id, templateType, checkType, checkProcess);
			result.setData(id);
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("添加或修改模型异常");
			ExceptionLogger.error("添加或修改模型 异常！", e.getMessage(), e);
		}
		return result;
	}

	@RequestMapping(value = "/getRankList.json")
	public RestServiceResult<List<Map<String, Object>>> getRankList(
			@RequestParam(value = "id", defaultValue = "", required = true) String id, HttpServletResponse response,
			HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
		// 获取当前用户信息
		/*
		 * RestServiceResult<SysUser> user =
		 * xypjRemoteBaseService.getCurrentUser(token); SysUser userInfo = null; if
		 * (RestServiceResult.CODE_SUCCESS == user.getCode()) { userInfo =
		 * user.getData(); if (null == userInfo || userInfo.getId() <= 0) {
		 * result.setCode(RestServiceResult.CODE_UNKNOW);
		 * result.setMsg("系统异常，请稍后再试！");
		 * ExceptionLogger.error("BatchLogController#getTableList",
		 * "未获取到当前用户信息！"); return result; } } else {
		 * result.setCode(user.getCode()); result.setMsg(user.getMsg()); return
		 * result; }
		 */
		try {
			List<Map<String, Object>> tableList = modelBaseService.getRankList(id);
			result.setData(tableList);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("获取规则列表异常，请稍后再试！");
			ExceptionLogger.error("获取规则列表异常！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/modelIndexCatTreeSave.do")
	public RestServiceResult<List<Map<String, Object>>> modelIndexCatTreeSave(
			@RequestParam(value = "tree", defaultValue = "", required = true) String tree, String modelId,
			HttpServletResponse response, HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		try {
			List<Map> listTree = JSON.parseObject(tree, new TypeReference<List<Map>>() {
			});
			modelBaseService.modelIndexCatTreeSave(listTree, modelId, String.valueOf(userInfo.getId()));
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("保存右侧树异常，请稍后再试！");
			ExceptionLogger.error("保存右侧树异常！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/updateWeight.do")
	public RestServiceResult<List<Map<String, Object>>> updateWeight(
			@RequestParam(value = "treeList", defaultValue = "", required = true) String treeList, String modelId,
			HttpServletResponse response, HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		try {
			List<IndexCatModel> list = JSON.parseObject(treeList, new TypeReference<List<IndexCatModel>>() {
			});
			modelBaseService.updateWeight(list, modelId);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("更新权重异常，请稍后再试！");
			ExceptionLogger.error("更新权重异常！", e.getMessage(), e);
			return result;
		}
	}

	public static void main(String[] args) {
		String treeList = "[{'standard':null,'level':'0','weight':'50','pid':'0','sort':'0','type':'category','indexCatModelId':'ee7d6f02e68f4c06ba85b81228aa1b1d','dictSource':'','indexType':'','des':'','extra':'','name':'财务','inputType':'','countRule':0,'id':'926591e86ffa4520bf8e736b2f8bf6481545890635233_1545890720928_1'},{'standard':null,'level':'1','weight':'40','pid':'926591e86ffa4520bf8e736b2f8bf6481545890635233_1545890720928_1','sort':'1','type':'index','indexCatModelId':'aac729a6541744eb83a8cc3d26cc6441','dictSource':null,'indexType':'3','des':'负债合计/资产总计','extra':'','name':'资产负债率','inputType':'1','countRule':3,'id':'52cd6f2b09a911e986f0000c29fcc91d'},{'standard':null,'level':'1','weight':'60','pid':'926591e86ffa4520bf8e736b2f8bf6481545890635233_1545890720928_1','sort':'2','type':'index','indexCatModelId':'a0c5a7cac0a64419a716ace14263d9dc','dictSource':null,'indexType':'3','des':'销售（应收）利润率','extra':'','name':'销售（应收）利润率','inputType':'1','countRule':3,'id':'52ce679b09a911e986f0000c29fcc91d'},{'standard':null,'level':'0','weight':'50','pid':'0','sort':'3','type':'category','indexCatModelId':'ef3308d70053459584cf5c146f39c16b','dictSource':'','indexType':'','des':'','extra':'','name':'证照分类','inputType':'','countRule':0,'id':'87bcd0a479d248fb8462efedec05981b_1545895461072_1'},{'standard':null,'level':'1','weight':'100','pid':'87bcd0a479d248fb8462efedec05981b_1545895461072_1','sort':'4','type':'index','indexCatModelId':'3f09191dff5540e0a49d33edefd93fc7','dictSource':null,'indexType':'6','des':'wukai证照','extra':'','name':'wukai证照','inputType':'H','countRule':0,'id':'52ce68a009a911e986f0000c29fcc91d'}]";
		treeList = treeList.replaceAll("/", "\\/");
		List<IndexCatModel> list = JSON.parseObject(treeList, new TypeReference<List<IndexCatModel>>() {
		});
	}

	@RequestMapping(value = "/getAllCatAndIndexes.json")
	public RestServiceResult<List<Map>> getAllCatAndIndexes(String mainType, HttpServletResponse response,
			HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		/*
		 * SysUser userInfo = null; if (RestServiceResult.CODE_SUCCESS ==
		 * user.getCode()) { userInfo = user.getData(); if (null == userInfo ||
		 * userInfo.getId() <= 0) {
		 * result.setCode(RestServiceResult.CODE_UNKNOW);
		 * result.setMsg("系统异常，请稍后再试！");
		 * ExceptionLogger.error("BatchLogController#getTableList",
		 * "未获取到当前用户信息！"); return result; } } else {
		 * result.setCode(user.getCode()); result.setMsg(user.getMsg()); return
		 * result; }
		 */
		try {
			List<Map> list = modelBaseService.getAllCatAndIndexes(mainType);
			result.setData(list);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("获取左侧菜单树异常，请稍后再试！");
			ExceptionLogger.error("获取左侧菜单树异常！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/getdefaultTree.json")
	public RestServiceResult<List<Map>> getdefaultTree(String modelId, HttpServletResponse response,
			HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		/*
		 * SysUser userInfo = null; if (RestServiceResult.CODE_SUCCESS ==
		 * user.getCode()) { userInfo = user.getData(); if (null == userInfo ||
		 * userInfo.getId() <= 0) {
		 * result.setCode(RestServiceResult.CODE_UNKNOW);
		 * result.setMsg("系统异常，请稍后再试！");
		 * ExceptionLogger.error("BatchLogController#getTableList",
		 * "未获取到当前用户信息！"); return result; } } else {
		 * result.setCode(user.getCode()); result.setMsg(user.getMsg()); return
		 * result; }
		 */
		try {
			List<Map> list = modelBaseService.getdefaultTree(modelId);
			result.setData(list);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("获取右侧默认树异常，请稍后再试！");
			ExceptionLogger.error("获取右侧默认树异常！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/getRules.json")
	public RestServiceResult<List<Map>> getRuleListByIndexModelCat(String indexCatModelId, String indexType,
			String dictSource, HttpServletResponse response, HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		// 获取当前用户信息
		/*
		 * RestServiceResult<SysUser> user =
		 * xypjRemoteBaseService.getCurrentUser(token); SysUser userInfo = null; if
		 * (RestServiceResult.CODE_SUCCESS == user.getCode()) { userInfo =
		 * user.getData(); if (null == userInfo || userInfo.getId() <= 0) {
		 * result.setCode(RestServiceResult.CODE_UNKNOW);
		 * result.setMsg("系统异常，请稍后再试！");
		 * ExceptionLogger.error("BatchLogController#getTableList",
		 * "未获取到当前用户信息！"); return result; } } else {
		 * result.setCode(user.getCode()); result.setMsg(user.getMsg()); return
		 * result; }
		 */
		try {
			List<Map> list = modelBaseService.getRuleListByIndexModelCat(indexCatModelId);
			if ((list == null || list.size() == 0) && StringUtils.isNotBlank(dictSource)) {
				list = modelBaseService.getRuleListByDict(dictSource);
			}
			result.setData(list);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("获取规则列表异常，请稍后再试！");
			ExceptionLogger.error("获取规则列表异常！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/getRulesForZz.json")
	public RestServiceResult<List<Map>> getRuleListByIndexModelCatForZz(String indexCatModelId, String indexType,
			String dictSource, String zzType, HttpServletResponse response, HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		// 获取当前用户信息
		/*
		 * RestServiceResult<SysUser> user =
		 * xypjRemoteBaseService.getCurrentUser(token); SysUser userInfo = null; if
		 * (RestServiceResult.CODE_SUCCESS == user.getCode()) { userInfo =
		 * user.getData(); if (null == userInfo || userInfo.getId() <= 0) {
		 * result.setCode(RestServiceResult.CODE_UNKNOW);
		 * result.setMsg("系统异常，请稍后再试！");
		 * ExceptionLogger.error("BatchLogController#getTableList",
		 * "未获取到当前用户信息！"); return result; } } else {
		 * result.setCode(user.getCode()); result.setMsg(user.getMsg()); return
		 * result; }
		 */
		try {
			List<Map> list = modelBaseService.getRuleListByIndexModelCatForZz(indexCatModelId, zzType);
			if ((list == null || list.size() == 0) && StringUtils.isNotBlank(dictSource)) {
				list = modelBaseService.getRuleListByDict(dictSource);
			}
			result.setData(list);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("获取规则列表异常，请稍后再试！");
			ExceptionLogger.error("获取规则列表异常！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/saveRules.do")
	public RestServiceResult<List<Map>> saveRules(String listRules, String indexCatModelId,
			HttpServletResponse response, HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		try {
			List<Map> list = JSON.parseObject(listRules, new TypeReference<List<Map>>() {
			});
			modelBaseService.saveRules(list, indexCatModelId);
			result.setData(list);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("保存规则异常，请稍后再试！");
			ExceptionLogger.error("保存规则异常！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/saveRanks.do")
	public RestServiceResult<List<Map>> saveRanks(String listRanks, String id, HttpServletResponse response,
			HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		try {
			List<Map> list = JSON.parseObject(listRanks, new TypeReference<List<Map>>() {
			});
			modelBaseService.saveRanks(list, id, String.valueOf(userInfo.getId()));
			result.setData(list);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("保存模型等级异常，请稍后再试！");
			ExceptionLogger.error("保存模型等级异常！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/getRelatedCategoryAndIndexes.json")
	public RestServiceResult<List<Map>> getRelatedCategoryAndIndexes(String catId, HttpServletResponse response,
			HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		// 获取当前用户信息
		/*
		 * RestServiceResult<SysUser> user =
		 * xypjRemoteBaseService.getCurrentUser(token); SysUser userInfo = null; if
		 * (RestServiceResult.CODE_SUCCESS == user.getCode()) { userInfo =
		 * user.getData(); if (null == userInfo || userInfo.getId() <= 0) {
		 * result.setCode(RestServiceResult.CODE_UNKNOW);
		 * result.setMsg("系统异常，请稍后再试！");
		 * ExceptionLogger.error("BatchLogController#getTableList",
		 * "未获取到当前用户信息！"); return result; } } else {
		 * result.setCode(user.getCode()); result.setMsg(user.getMsg()); return
		 * result; }
		 */
		try {
			List<Map> list = modelBaseService.getRelatedCategoryAndIndexes(catId);
			result.setData(list);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("获取关联模型分类树异常，请稍后再试！");
			ExceptionLogger.error("获取关联模型分类树异常！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/getWeightList.json")
	public RestServiceResult<List<Map>> getRelatedCategoryAndIndexesByModelId(String modelId,
			HttpServletResponse response, HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		// 获取当前用户信息
		/*
		 * RestServiceResult<SysUser> user =
		 * xypjRemoteBaseService.getCurrentUser(token); SysUser userInfo = null; if
		 * (RestServiceResult.CODE_SUCCESS == user.getCode()) { userInfo =
		 * user.getData(); if (null == userInfo || userInfo.getId() <= 0) {
		 * result.setCode(RestServiceResult.CODE_UNKNOW);
		 * result.setMsg("系统异常，请稍后再试！");
		 * ExceptionLogger.error("BatchLogController#getTableList",
		 * "未获取到当前用户信息！"); return result; } } else {
		 * result.setCode(user.getCode()); result.setMsg(user.getMsg()); return
		 * result; }
		 */
		try {
			List<Map> list = modelBaseService.getRelatedCategoryAndIndexesByModelId(modelId);
			result.setData(list);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("获取权重列表异常，请稍后再试！");
			ExceptionLogger.error("获取权重列表异常！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/getRelatedCategoryAndIndexesTreeByModelId.json")
	public RestServiceResult<List<Map>> getRelatedCategoryAndIndexesTreeByModelId(String modelId,
			HttpServletResponse response, HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		// 获取当前用户信息
		/*
		 * RestServiceResult<SysUser> user =
		 * xypjRemoteBaseService.getCurrentUser(token); SysUser userInfo = null; if
		 * (RestServiceResult.CODE_SUCCESS == user.getCode()) { userInfo =
		 * user.getData(); if (null == userInfo || userInfo.getId() <= 0) {
		 * result.setCode(RestServiceResult.CODE_UNKNOW);
		 * result.setMsg("系统异常，请稍后再试！");
		 * ExceptionLogger.error("BatchLogController#getTableList",
		 * "未获取到当前用户信息！"); return result; } } else {
		 * result.setCode(user.getCode()); result.setMsg(user.getMsg()); return
		 * result; }
		 */
		try {
			List<Map> list = modelBaseService.getRelatedCategoryAndIndexesTreeByModelId(modelId);
			result.setData(list);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("根据模型加载分类树异常，请稍后再试！");
			ExceptionLogger.error("据模型加载分类树异常！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/del.do")
	public RestServiceResult<List<Map>> del(@RequestParam(value = "ids", defaultValue = "", required = true) String ids,
			HttpServletResponse response, HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		try {
			if(!StringUtils.isEmpty(modelBaseService.selectMXSQ(ids))) {
				result.setMsg("授权模型使用了该模型,不能删除！");
				return result;
			}
			modelBaseService.del(ids);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("删除数据异常，请稍后再试！");
			ExceptionLogger.error("删除数据异常！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/setStatus.do")
	public RestServiceResult<List<Map>> setStatus(String status, String id, HttpServletResponse response,
			HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		try {
			modelBaseService.setStatus(status, id);
			indexService.updateStatusBymodelId(status, id);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg(e.getMessage());
			ExceptionLogger.error("设置状态失败！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/updateStandard.do")
	public RestServiceResult<List<Map>> updateStandard(String indexCatModelId, String standard,
			HttpServletResponse response, HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		try {
			modelBaseService.updateStandard(indexCatModelId, standard);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("更新标准失败，请稍后再试！");
			ExceptionLogger.error("更新标准失败！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/selectOne.json")
	public RestServiceResult<Map> selectOne(String id, HttpServletResponse response, HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<Map> result = new RestServiceResult<Map>();
		// 获取当前用户信息
		/*
		 * RestServiceResult<SysUser> user =
		 * xypjRemoteBaseService.getCurrentUser(token); SysUser userInfo = null; if
		 * (RestServiceResult.CODE_SUCCESS == user.getCode()) { userInfo =
		 * user.getData(); if (null == userInfo || userInfo.getId() <= 0) {
		 * result.setCode(RestServiceResult.CODE_UNKNOW);
		 * result.setMsg("系统异常，请稍后再试！");
		 * ExceptionLogger.error("BatchLogController#getTableList",
		 * "未获取到当前用户信息！"); return result; } } else {
		 * result.setCode(user.getCode()); result.setMsg(user.getMsg()); return
		 * result; }
		 */
		try {
			Map m = modelBaseService.selectOne(id);
			result.setData(m);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("加载数据失败，请稍后再试！");
			ExceptionLogger.error("加载数据失败！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/validateBm.json")
	public RestServiceResult<Integer> validateBm(String id, String bm, HttpServletResponse response,
			HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<Integer> result = new RestServiceResult<Integer>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		try {
			Integer count = modelBaseService.validateBm(id, bm);
			result.setData(count);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("加载数据失败，请稍后再试！");
			ExceptionLogger.error("加载数据失败！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/validateModel.json")
	public RestServiceResult<Integer> validateModel(String id, String zone, String industry, String reportType,
			String mainType, HttpServletResponse response, HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<Integer> result = new RestServiceResult<Integer>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		try {
			Integer count = modelBaseService.validateModel(id, zone, industry, reportType, mainType);
			result.setData(count);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("加载数据失败，请稍后再试！");
			ExceptionLogger.error("加载数据失败！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/validateSetStatus.json")
	public RestServiceResult<Integer> validateSetStatus(String id, String zone, String industry, String reportType,
			HttpServletResponse response, HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<Integer> result = new RestServiceResult<Integer>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		try {
			Integer count = modelBaseService.validateSetStatus(id);
			result.setData(count);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("加载数据失败，请稍后再试！");
			ExceptionLogger.error("加载数据失败！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/selectModelForSelect.json")
	public RestServiceResult<List<Map>> selectModelForSelect(String mainType, HttpServletResponse response,
			HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		// 获取当前用户信息
		/*
		 * RestServiceResult<SysUser> user =
		 * xypjRemoteBaseService.getCurrentUser(token); SysUser userInfo = null; if
		 * (RestServiceResult.CODE_SUCCESS == user.getCode()) { userInfo =
		 * user.getData(); if (null == userInfo || userInfo.getId() <= 0) {
		 * result.setCode(RestServiceResult.CODE_UNKNOW);
		 * result.setMsg("系统异常，请稍后再试！");
		 * ExceptionLogger.error("BatchLogController#getTableList",
		 * "未获取到当前用户信息！"); return result; } } else {
		 * result.setCode(user.getCode()); result.setMsg(user.getMsg()); return
		 * result; }
		 */
		try {
			List<Map> list = modelBaseService.selectModelForSelect(mainType);
			result.setData(list);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("获取模型下拉列表异常，请稍后再试！");
			ExceptionLogger.error("获取模型下拉列表异常！", e.getMessage(), e);
			return result;
		}
	}
	@RequestMapping(value = "/selectTemplateMap.json")
	public RestServiceResult<List<Map>> selectTemplateMap(
			HttpServletResponse response,HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		try {
			result.setData(modelBaseService.selectTemplateMap());
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("更新标准失败，请稍后再试！");
			ExceptionLogger.error("更新标准失败！", e.getMessage(), e);
			return result;
		}
	}
	

	@RequestMapping(value = "/selectModelSelect.json")
	public RestServiceResult<List<Map>> selectModelSelect(String ywName, HttpServletResponse response,
			HttpServletRequest request) {
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		
		try {
			List<Map> list;
			if("1".equals(ywName)){
				list = modelBaseService.selectModelForSelect("100000");
			}else{
				list = modelBaseService.selectModelForSelect("");
			}
			 
			result.setData(list);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("获取模型下拉列表异常，请稍后再试！");
			ExceptionLogger.error("获取模型下拉列表异常！", e.getMessage(), e);
			return result;
		}
	}
}
